package com.coocaa.x.provider.xprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;

public class XContentProvider extends ContentProvider {
    private static final String ERROR_PREFIX = "Unkwon Uri:";
    private static final UriMatcher MATCHER = new UriMatcher(
            UriMatcher.NO_MATCH);
    private static final PROVIDERS[] ps = PROVIDERS.values();

    @Override
    public boolean onCreate() {
        CoocaaApplication.setContext(getContext());
        LogUtils.d("sss", "XContentProvider onCreate Thread:" + Thread.currentThread());
        XProviderManager.init(getContext());
        for (PROVIDERS p : ps) {
            if (p.handler != null && p.handler.onCreate(getContext()))
                MATCHER.addURI(XProviderManager.URI_AUTHORITY, p.path, p.ordinal() + 1);
        }
        return true;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code = MATCHER.match(uri);
        if (code > 0) {
            try {
                return ps[code - 1].handler.delete(uri, selection, selectionArgs);
            } catch (IProviderHandler.ProviderHandlerNotHandledException e) {
                throw new IllegalArgumentException(e);
            }
        }
        throw new IllegalArgumentException(ERROR_PREFIX + uri.toString());
    }

    @Override
    public String getType(Uri uri) {
        int code = MATCHER.match(uri);
        if (code > 0) {
            try {
                return ps[code - 1].handler.getType(uri);
            } catch (IProviderHandler.ProviderHandlerNotHandledException e) {
                throw new IllegalArgumentException(e);
            }
        }
        throw new IllegalArgumentException(ERROR_PREFIX + uri.toString());
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code = MATCHER.match(uri);
        if (code > 0) {
            try {
                return ps[code - 1].handler.insert(uri, values);
            } catch (IProviderHandler.ProviderHandlerNotHandledException e) {
                throw new IllegalArgumentException(e);
            }
        }
        throw new IllegalArgumentException(ERROR_PREFIX + uri.toString());
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        int code = MATCHER.match(uri);
        if (code > 0) {
            try {
                return ps[code - 1].handler.query(uri, projection, selection, selectionArgs, sortOrder);
            } catch (IProviderHandler.ProviderHandlerNotHandledException e) {
                throw new IllegalArgumentException(e);
            }
        }
        throw new IllegalArgumentException(ERROR_PREFIX + uri.toString());
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        int code = MATCHER.match(uri);
        if (code > 0) {
            try {
                return ps[code - 1].handler.update(uri, values, selection, selectionArgs);
            } catch (IProviderHandler.ProviderHandlerNotHandledException e) {
                throw new IllegalArgumentException(e);
            }
        }
        throw new IllegalArgumentException(ERROR_PREFIX + uri.toString());
    }
}
